home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / curses / crssrc12.zoo / src / tgetent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  15.8 KB  |  630 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tgetent   load buffer with entry for specified terminal
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap functions
  28.  *    utility routines
  29.  *
  30.  *  SYNOPSIS
  31.  *
  32.  *    int tgetent(bp,name)
  33.  *    char *bp;
  34.  *    char *name;
  35.  *
  36.  *  DESCRIPTION
  37.  *
  38.  *    Extracts the entry for terminal <name> from the termcap file
  39.  *    and places it in the character buffer <bp>.   It is currently
  40.  *    assumed that bp is at least 1024 characters.  If the entry in
  41.  *    the termcap file is larger than 1023 characters the excess
  42.  *    characters will be discarded and appropriate status will
  43.  *    be returned.
  44.  *
  45.  *    Also note that since bp is used by other termcap
  46.  *    routines, the storage associated with the termcap entry
  47.  *    cannot be freed until all termcap calls are completed.
  48.  *
  49.  *    Tgetent can be directed to look in a file other than
  50.  *    the default (/etc/termcap) by defining an environment
  51.  *    variable called TERMCAP to be the pathname of the desired
  52.  *    termcap file.  This is useful for debugging new entries.
  53.  *    NOTE: the pathname MUST begin with a '/' character.
  54.  *      (Atari ST specific change: the pathname may begin with '\',
  55.  *       or with a drive letter followed by ':').
  56.  *
  57.  *    Also, if the string assigned to TERMCAP does not begin with
  58.  *    a '/' and if the environment variable TERM matches <name> then
  59.  *    the string assigned to TERMCAP is copied to buffer <bp> 
  60.  *    instead of reading a termcap file.
  61.  *      (Atari ST specific change: TERM is no longer checked (the
  62.  *       check was buggy).
  63.  *
  64.  *    Modification by ERS: if no termcap file can be found, then
  65.  *    a default termcap is used (this is for GEMDOS).
  66.  *
  67.  *      Further mods by MJ: original routines fail to proces valid
  68.  *      termcap files - replaced with new versions.
  69.  *      Atari specific: default termcap used when nothing better is
  70.  *      around reads a number of rows and colums from Line-A variables.
  71.  *    
  72.  *  RETURNS
  73.  *
  74.  *    -1  if the termcap file cannot be opened
  75.  *     0  if no entry in termcap file matches <name>
  76.  *     1  if extraction is successful with no errors
  77.  *     2  if extraction is successful but entry truncated
  78.  *
  79.  *  SEE ALSO
  80.  *
  81.  *    tgetnum   extract numeric type capability
  82.  *    tgetflag  test boolean type capability
  83.  *    tgetstr   get string value of capability
  84.  *
  85.  *  AUTHOR
  86.  *
  87.  *    Fred Fish
  88.  *
  89.  */
  90.  
  91. #include <stdio.h>
  92. #include <string.h>
  93. #include <termcap.h>
  94. #include <ctype.h>
  95.  
  96. #ifndef _COMPILER_H
  97. #  include <compiler.h>
  98. #endif
  99.  
  100. #define TRUE 1
  101. #define FALSE 0
  102. #define BUFSIZE 1024            /* Assumed size of external buffer */
  103.  
  104. #define NO_FILE     -1            /* Returned if can't open file */
  105. #define NO_ENTRY  0            /* Returned if can't find entry */
  106. #define SUCCESS   1            /* Returned if entry found ok */
  107. #define TRUNCATED 2            /* Returned if entry found but trunc */
  108.  
  109. # ifdef DGK
  110. # define DEFAULT_ROOT "termcap"        /* name without path component */
  111. /**
  112.   static FILE *fopenp __PROTO((char *name, char *mode, char *pathname));
  113. **/
  114. # define DEFAULT_FILE "\\etc\\termcap"
  115. # else
  116. # define DEFAULT_FILE "/etc/termcap"    /* default termcap filename */
  117. # endif
  118. /* __EXTERN char *fgetlr __PROTO((char *bp, int bpsize, FILE *fp)); */
  119. static int build_entry __PROTO((char *bp, char *stop, FILE *fp, char *name));
  120. static FILE *find_file __PROTO((char *));
  121. /**
  122. static int gotcha __PROTO((char *bp, char *name));
  123. **/
  124.  
  125. char *_tcpbuf;                /* Place to remember buffer pointer */
  126.  
  127. /*
  128.  *  PSEUDO CODE
  129.  *
  130.  *    Begin tgetent
  131.  *        Erase any previous buffer contents.
  132.  *        Remember the buffer pointer.
  133.  *        If termcap file is not found then
  134.  *        If buffer was filled anyway then
  135.  *            Return SUCCESS.
  136.  *        Else
  137.  *            Return NO_FILE.
  138.  *        End if
  139.  *        Else
  140.  *        While records left to process
  141.  *            If this is entry is what we want then
  142.  *            Close the termcap file.
  143.  *            If entry was truncated then
  144.  *                Return TRUNCATED status
  145.  *            Else
  146.  *                Return SUCCESS status.
  147.  *            End if
  148.  *            End if
  149.  *        End while
  150.  *        Return NO_ENTRY status.
  151.  *        End if
  152.  *    End tgetent
  153.  *
  154.  *    Modification by Michal Jagerman (April of 1992):
  155.  *      The "While records left to process" is too simple minded
  156.  *      since termcap entry in a buffer can be spliced from
  157.  *    multiple pieces by using "tc" capability. Therefore
  158.  *      we delegete the task of termcap buffer filling to
  159.  *      a specialized internal function 'build_entry()'. This
  160.  *      function also solves a problem of opening a termcap file
  161.  *      in a 'wrong' mode by cleaning a termcap buffer from all
  162.  *      leftovers.
  163.  *
  164.  *  
  165.  */
  166.  
  167. int tgetent(bp,name)
  168. char *bp;                /* Pointer to buffer (1024 char min) */
  169. char *name;                /* Pointer to terminal entry to find */
  170. {
  171.     FILE *fp;
  172.  
  173.     *bp = '\0';
  174.     _tcpbuf = bp;
  175.     if ((fp = find_file(bp)) == NULL) {
  176.     if (*bp != '\0') {
  177.         return(SUCCESS);
  178.     } else {
  179.         return(NO_FILE);
  180.     }
  181.     } else {
  182.     *bp++ = ':';
  183.     return (build_entry(bp, bp + BUFSIZE - 1, fp, name));
  184.     }
  185. }
  186.  
  187. /*
  188.  *  INTERNAL FUNCTION
  189.  *
  190.  *    build_entry    construct termcap entry in a given buffer
  191.  *
  192.  *  SYNOPSIS
  193.  *
  194.  *    static int build_entry(bp, stop, fp, name)
  195.  *    char *bp, *stop, *name;
  196.  *      FILE *fp;
  197.  *
  198.  *  DESCRIPTION
  199.  *
  200.  *      For a given name build in a buffer bp a termcap description
  201.  *      using a contents of file fp.  Continue this until the entry
  202.  *      is complete or we reached stop.  Concatenate entries if
  203.  *      required by tc capability.   White space characters and
  204.  *      backslashes escaping newlines are not copied into bp.
  205.  *      Returns SUCCESS if there was no problems, NO_ENTRY if an
  206.  *      entry with given name was not found and TRUNCATED if we
  207.  *      run out of a buffer space or continuation entry specified
  208.  *      with tc was not found
  209.  *
  210.  *  BUGS
  211.  *
  212.  *      Termcap specifications require for tc to be the last capability
  213.  *      for the given name.  This is not enforced but anything which
  214.  *      follows tc in the same description will be discarded.
  215.  *      It is not entirely clear what we should return when continuation
  216.  *      specified with tc failed.
  217.  *      Other stuff which goes beyond termcap specs can be accepted.
  218.  *      Terminal names starting with '#' are not accepted.
  219.  *      Continuation with names over 127 characters long will likely bomb!
  220.  *
  221.  *  AUTHOR
  222.  *
  223.  *    Michal Jaegermann
  224.  *
  225.  */
  226. static int build_entry(bp, stop, fp, name)
  227. char *bp, *stop, *name;
  228. FILE *fp;
  229. {
  230.     int c;
  231.     int so_far, skip_all = 0;
  232.     char *np;
  233.     char nbuf[128];
  234.     static int _tgetc __PROTO((FILE *fp));
  235.  
  236.     /* rewind file - we may seek for a continuation entry */
  237.     rewind(fp);
  238.  
  239.     /*
  240.      * this is FSM - sort of 
  241.      */
  242.     while (EOF != (c = getc(fp))) {
  243.      /*
  244.       * we are looking at a comment - skip it
  245.       */
  246.     if ('#' == c) {
  247.         do {
  248.         if (EOF == (c = getc(fp)))
  249.             return NO_ENTRY;
  250.         } while ('\n' != c);
  251.     }
  252.     /*
  253.      * empty line or we finished comment traversal;
  254.      * a little bit to good - but valid termcap file will be
  255.      * stil accepted
  256.      */
  257.     if (isspace(c))
  258.         continue;
  259.     /*
  260.      * try matching name
  261.      */
  262.     np = name;
  263.     while (*np == c) {
  264.         np += 1;
  265.         c = _tgetc(fp);
  266.     }
  267.     /*
  268.      * we finished traversing our name - is this really a match ?
  269.      */
  270.     if (*np == '\0') {
  271.         if (c == '|' || c == ':')
  272.         break; /* we have a match */
  273.         if  (c == EOF)
  274.         return (TRUNCATED); /* match - but we wanted more */
  275.     }
  276.     /*
  277.      * no match - skip until next name or next entry
  278.      * if we are past all possible names here 
  279.      */
  280.     skip_all = 0;
  281.     while ('\n' != c) {
  282.         if (':' == c)
  283.         skip_all = 1; /* we are past all valid names for this entry */
  284.         if ('|' == c && 0 == skip_all)
  285.         break;
  286.         c = _tgetc(fp);
  287.     }
  288.     }
  289.     if (EOF == c)
  290.     return (NO_ENTRY);
  291.     while (':' != c)    /* skip the remainig variants of terminal names */
  292.     c = _tgetc(fp); /* we do not want any mixups later             */
  293.  
  294.     /*
  295.      * at last we got it - copy terminal description into bp
  296.      */
  297.     so_far = 0;  /* rough indicator how far we are into a capability */
  298.     while ('\n' != (c = _tgetc(fp))) {
  299.     if (0 == so_far && !isalpha(c))
  300.         continue;     /* do not bother with all kind of spurious stuff */
  301.     so_far++;
  302.     if (1 == so_far && 't' == c ) {
  303.         /* a special case - maybe we have "tc=" string? */
  304.         if ((bp + 3) > stop) {
  305.         ungetc(c, fp);
  306.         continue;
  307.         /* cheating with so_far, but we want to skip this case! */
  308.         }
  309.         *bp++ = c;
  310.         c = _tgetc(fp);
  311.         if ('c' == c) {
  312.         *bp++ = c;
  313.         c = _tgetc(fp);
  314.         if ('=' == c) {
  315.             /* we will continue with a name which follows */
  316.             bp -= 2;
  317.             /* copy new name to nbuf */
  318.             np = nbuf;
  319.             while (':' != (c = _tgetc(fp))) {
  320.             if ('\n' == c || EOF == c)
  321.                 break;
  322.             *np++ = c;
  323.             }
  324.             *np = '\0';
  325.             return (SUCCESS == build_entry(bp, stop, fp, nbuf) ?
  326.                 SUCCESS : TRUNCATED);
  327.         }
  328.         }
  329.     }  /* end of 'tc=' check */
  330.     if (':' == c) /* literal colon cannot occur in capabilities strings -
  331.                * one has to use '\072' instead */
  332.         so_far =  0;
  333.     *bp++ = c;
  334.     if (bp >= stop)
  335.         return(TRUNCATED);
  336.     }
  337.     if (bp < stop)
  338.     *bp = '\0';
  339.     return(SUCCESS);
  340. }
  341.  
  342. /*
  343.  * Auxilary function to read a character from a text file
  344.  * with skipping escaped line terminators; any escaped
  345.  * '\n' will be replaced by a character which follows.
  346.  * After escape any number of ^M's will vanish,
  347.  * i.e a string of three characters '\\', 0x0d, 'a' will read
  348.  * as a string of two characters '\\', 'a' and so on...
  349.  * We do not tolerate such garbage in text files. :-)
  350.  */
  351. static int _tgetc(fp)
  352. FILE *fp;
  353. {
  354.     int c;
  355.  
  356.     if ('\\' == (c = getc(fp))) {
  357.     while ('\r' == (c = getc(fp)))
  358.         ;                /* Messy stuff - go away */
  359.     if (c != '\n') {
  360.         ungetc(c, fp);
  361.         return ('\\');
  362.     }
  363.     c = getc(fp);
  364.     }
  365.     return(c);
  366. }
  367.  
  368.  
  369.  
  370. /*
  371.  *  INTERNAL FUNCTION
  372.  *
  373.  *    find_file    find the termcap file and open it if possible
  374.  *
  375.  *  KEY WORDS
  376.  *
  377.  *    internal functions
  378.  *    find_file
  379.  *
  380.  *  SYNOPSIS
  381.  *
  382.  *    static FILE *find_file(bp)
  383.  *    char *bp;
  384.  *
  385.  *  DESCRIPTION
  386.  *
  387.  *    Attempts to locate and open the termcap file.  Also handles
  388.  *    using the environment TERMCAP string as the actual buffer
  389.  *    (that's why bp has to be an input parameter).
  390.  *
  391.  *    If TERMCAP is defined an begins with a '/' character then
  392.  *    it is taken to be the pathname of the termcap file and
  393.  *    an attempt is made to open it.  If this fails then
  394.  *    the default termcap file is used instead.
  395.  *
  396.  *    If TERMCAP is defined but does not begin with a '/' then
  397.  *    it is assumed to be the actual buffer contents provided
  398.  *    that <name> matches the environment variable TERM.
  399.  *
  400.  *  BUGS
  401.  *
  402.  *    There is currently no way to be sure which termcap
  403.  *    file was opened since the default will always be
  404.  *    tried.
  405.  *
  406.  */
  407.  
  408. /*
  409.  *  PSEUDO CODE
  410.  *
  411.  *    Begin find_file
  412.  *        If there is a TERMCAP environment string then
  413.  *        If the string is not null then
  414.  *            If the string is a pathname then
  415.  *            If that file is opened successfully then
  416.  *                Return its pointer.
  417.  *            End if
  418.  *            Else
  419.  *            If there is a TERM environment string then
  420.  *                If TERM matches <name> then
  421.  *                Copy TERMCAP string to buffer.
  422.  *                Return NULL for no file.
  423.  *                End if
  424.  *            End if
  425.  *            End if
  426.  *        End if
  427.  *        End if
  428.  *        Open default termcap file and return results.
  429.  *    End find_file
  430.  *
  431.  */
  432.  
  433. #ifdef GEMDOS
  434. /*
  435.  * we do not really need the following part once the stuff is in
  436.  * our termcap  buffer
  437.  */
  438. /* "df|default|Atari default"  */
  439. /*
  440.  * this values we will try to fill to fit a given display
  441.  */
  442. /* :co#80:li#25:\  */
  443.  
  444. static char term_default[] = "\
  445. :al=\\EL:am:bs:cd=\\EJ:ce=\\EK:cl=\\EE:cm=\\EY%+ %+ :dl=\\EM\
  446. :do=\\EB:eo:ho=\\EH:is=\\Eq\\EE\\Ee\\Ev:it#8:pt:kb=^H:ll=\\EY9!\
  447. :me=\\Eq:mr=\\Ep:le=\\ED:nd=\\EC:rc=\\Ek:sc=\\Ej:se=\\Eq:so=\\Ep:ta=^I\
  448. :up=\\EA:ve=\\Ee:vi=\\Ef:km:bl=^G:cr=^M:ti=\\Ev\\Ee:sr=\\EI:sf=^J";
  449.  
  450. #include <linea.h>
  451. #include <support.h>
  452. #endif
  453.  
  454. static FILE *find_file(bp)
  455. char *bp;
  456. {
  457.     FILE *fp;
  458.     char *cp, *ncp;
  459.     __EXTERN char *getenv __PROTO((const char *));
  460.  
  461.     if ((cp = getenv("TERMCAP")) != NULL) {
  462.     if (*cp != '\0') {
  463.         if (*cp == '/' || *cp == '\\' || (cp[1] == ':')) {
  464.         if ((fp = fopen(cp,"r")) != NULL) {
  465.             return(fp);
  466.         }
  467.         } else {
  468.         if ((ncp = getenv("TERM")) != NULL) {
  469.             strcpy(bp,cp);
  470.             return((FILE *)NULL);
  471.         }
  472.         }
  473.     }
  474.     }
  475. # ifdef DGK
  476.     /* Try current directory, then /etc/termcap, then along the path
  477.      */
  478.     if (fp = fopen(DEFAULT_ROOT, "r"))
  479.         return fp;
  480.     else if (fp = fopen(DEFAULT_FILE, "r"))
  481.         return fp;
  482.         else if (NULL !=
  483.          (cp = findfile(DEFAULT_ROOT, getenv("PATH"), (char **)0)) &&
  484.          (NULL != (fp = fopen(cp,"r"))))
  485.                return fp;
  486.             
  487. /** this replaced by the above **
  488.     else if (fp = fopenp(DEFAULT_ROOT, "r", NULL))
  489.         return fp;
  490. **/
  491.     else {
  492. #  ifdef GEMDOS
  493.         /* 
  494.          * if we do not have any better information, then
  495.          * we will try to glimpse screen sizes from Line-A variables
  496.          */
  497.         linea0();
  498.         strcpy (bp, ":co#");
  499.         (void) _ultoa((unsigned long) (V_CEL_MX + 1),
  500.                   bp + sizeof(":co#") - 1, 10);
  501.         strcat (bp, ":li#");
  502.         (void) _ultoa((unsigned long) (V_CEL_MY + 1),
  503.                   bp + strlen(bp), 10);
  504.         strcat (bp, term_default);
  505. #  endif
  506.         return (FILE *) NULL;
  507.     }
  508. # else
  509.     return(fopen(DEFAULT_FILE,"r"));
  510. # endif
  511. }
  512. #if 0  /* this is not used anymore */
  513.  
  514. /*
  515.  *  INTERNAL FUNCTION
  516.  *
  517.  *    gotcha   test to see if entry is for specified terminal
  518.  *
  519.  *  SYNOPSIS
  520.  *
  521.  *    gotcha(bp,name)
  522.  *    char *bp;
  523.  *    char *name;
  524.  *
  525.  *  DESCRIPTION
  526.  *
  527.  *    Tests to see if the entry in buffer bp matches the terminal
  528.  *    specified by name.  Returns TRUE if match is detected, FALSE
  529.  *    otherwise.
  530.  *
  531.  */
  532.  
  533. /*
  534.  *  PSEUDO CODE
  535.  *
  536.  *    Begin gotcha
  537.  *        If buffer character is comment character then
  538.  *        Return FALSE since remainder is comment
  539.  *        Else
  540.  *        Initialize name scan pointer.
  541.  *        Compare name and buffer until end or mismatch.
  542.  *        If valid terminators for both name and buffer strings
  543.  *            Return TRUE since a match was found.
  544.  *        Else
  545.  *            Find next non-name character in buffer.
  546.  *            If not an alternate name separater character
  547.  *            Return FALSE since no more names to check.
  548.  *            Else
  549.  *            Test next name and return results.
  550.  *            End if
  551.  *        End if
  552.  *        End if
  553.  *    End gotcha
  554.  *
  555.  */
  556.  
  557. static int gotcha(bp,name)
  558. char *bp;
  559. char *name;
  560. {
  561.     char *np;
  562.  
  563.     if (*bp == '#') {
  564.     return(FALSE);
  565.     } else {
  566.     np = name;
  567.     while (*np == *bp && *np != '\0') {np++; bp++;}
  568.     if (*np == '\0' && (*bp == '\0' || *bp == '|' || *bp == ':')) {
  569.         return(TRUE);
  570.     } else {
  571.         while (*bp != '\0' && *bp != ':' && *bp != '|') {bp++;}
  572.         if (*bp != '|') {
  573.         return(FALSE);
  574.         } else {
  575.         return(gotcha(++bp,name));
  576.         }
  577.     }
  578.     }
  579. }
  580.  
  581. #ifdef DGK
  582. # ifdef MSDOS
  583. # define PATHSEP ';'
  584. # endif
  585. # ifdef GEMDOS
  586. # define PATHSEP ','
  587. # endif
  588.  
  589. /* Follow the PATH, trying to fopen the file.  Takes one additional
  590.  * argument which can be NULL.  Otherwise this argument gets filled
  591.  * in the full path to the file.  Returns as does fopen().
  592.  */
  593.  
  594. /* On Atari ST use library routine findfile() instead */
  595.  
  596. static FILE *
  597. fopenp(name, mode, pathname)
  598. char *name, *mode, *pathname;
  599. {
  600.     char buffer[BUFSIZ], *buf, *bufp, *pathp, lastch;
  601.     FILE *fp;
  602.     __EXTERN char *getenv __PROTO((const char *));
  603.  
  604.     /* If pathname is given, use it instead of buf so the calling
  605.      * process knows the path we found name under
  606.      */
  607.     if (pathname)
  608.         buf = pathname;
  609.     else
  610.         buf = buffer;
  611.  
  612.     strcpy(buf, name);
  613.     pathp = getenv("PATH");
  614.     while (pathp && *pathp) {
  615.         bufp = buf;
  616.         while (*pathp && *pathp != PATHSEP)
  617.             lastch = *bufp++ = *pathp++;
  618.         if (lastch != '\\')
  619.             *bufp++ = '\\';
  620.         strcpy(bufp, name);
  621.         if (fp = fopen(buf, mode))
  622.             return fp;
  623.         if (*pathp)
  624.             pathp++;
  625.     }
  626.     return NULL;
  627. }
  628. #endif
  629. #endif /* 0 */
  630.